$.admin = function () {

    /**
     * 变量参数
     */
    var ops = {
        /**
         * 主页标签名称
         */
        homeTitle: null,
        /**
         * 主页标签地址
         */
        homeUrl: null,
        /**
         * 打开标签最大数量
         */
        maxTabCount: null,
        /**
         * 菜单显示方式 tree:树形 accordion:手风琴
         */
        menuType: null,
        /**
         * 菜单加载提示信息
         */
        menuMask: null,
        /**
         * 菜单Url
         */
        menuUrl: null,
        /**
         * 菜单数据
         */
        menuData: null,
        /**
         * 是否是树形菜单
         */
        isTreeMenu: null,
        $body: null,
        $north: null,
        $west: null,
        $center: null,
        $south: null,
        $menu: null,
        $tabs: null,
        $time: null,
        $tabsmenu: null
    };

    /**
     * 初始化变量
     */
    function initVar() {
        ops.$body = $('body');
        ops.$north = $('#layout-north');
        ops.$west = $('#layout-west');
        ops.$center = $('#layout-center');
        ops.$south = $('#layout-south');
        ops.$menu = $('#layout-west-menu');
        ops.$tabs = $('#layout-center-tabs');
        ops.$time = $('#layout-south-user-time');
        ops.$tabsmenu = $('#layout-center-tabs-menu');
        ops.homeTitle = ops.$tabs.data('homeTitle') || '我的桌面';
        ops.homeUrl = ops.$tabs.data('homeUrl') || 'home.html';
        ops.maxTabCount = ops.$tabs.data('maxTabCount') || 10;
        ops.menuMask = ops.$menu.data('mask') || '正在加载菜单...';
        ops.menuType = ops.$menu.data('type') || 'tree';
        ops.menuUrl = ops.$menu.data('url');
        ops.isTreeMenu = ops.menuType == 'tree';
    }

    function createFrame(url) {
        return '<iframe scrolling="auto" frameborder="0" style="width:100%;height:100%;" src="' + url + '" ></iframe>';
    }

    /**
     * 初始化时间
     */
    function initTime() {
        setInterval(function () {
            ops.$time.html(new Date().formatDateTime());
        }, 1000);
    }

    /**
     * 初始化标签
     */
    function initTabs() {
        ops.$tabs.tabs({
            border: false,
            fit: true,
            onContextMenu: onTabsContextMenu,
            onSelect: setHash
        });
        ops.$tabs.tabs('add', {
            id: 'home',
            title: ops.homeTitle,
            iconCls: 'icon-home',
            bodyCls: 'tabs-body',
            content: createFrame(ops.homeUrl),
            closable: false
        });
        ops.$tabsmenu.menu({onClick: tabsMenuClick});
    }

    function initLayout() {
        ops.$body.layout();
    }

    function initMenu() {
        if (ops.isTreeMenu) {
            ops.$west.attr('title', "导航菜单")
        }
        $.ajax({
            type: 'get',
            url: ops.menuUrl,
            beforeSend: function () {
                ops.$west.mask(ops.menuMask, 200);
            },
            success: buildMenu
        });
    }

    function buildMenu(data) {
        ops.$west.unmask();
        if (!data || !data.length) {
            return;
        }
        ops.menuData = data;
        if (ops.isTreeMenu) {
            buildTreeMenu(data);
        }
        else {
            buildAccordionMenu(data);
        }
    }

    function buildTreeMenuCore($selector, data) {
        $selector.tree({
            data: data,
            onLoadSuccess: function (n, data) {
                // var menuId = window.location.hash.replace('#', '');
                // if (menuId) {
                //     var node = $(this).tree('find', menuId);
                //     if (node) {
                //         if (index.settings.navigation == 'accordion') {
                //             $('#wnav').accordion('select', $(this).data('accordion'));
                //         }
                //         $(this).tree('expandTo', node.target);
                //         $(this).tree('select', node.target);
                //         index.addTab(node);
                //     }
                // }
            },
            onClick: function (node) {
                $(this).tree('expand', node.target);
                if (node.url) {
                    addTab(node);
                }
            },
            onDblClick: function (node) {
                if (node.state === 'open') {
                    $(this).tree('collapse', node.target);
                }
            },
            onContextMenu: function (e, node) {
                $(this).tree('select', node.target);
                e.preventDefault();
            }
        });
    }

    function buildTreeMenu(data) {
        ops.$menu.append('<ul class="menuTree"></ul>');
        buildTreeMenuCore(ops.$menu.find('.menuTree'), data);
    }

    function buildAccordionMenu(data) {
        ops.$menu.accordion({animate: false, fit: true, border: false});
        for (var i = 0; i < data.length; i++) {
            var item = data[i];
            ops.$menu.accordion('add', {
                title: item.text,
                content: '<div><ul class="navTree" data-accordion="' + i + '"></ul></div>',
                // iconCls: 'icon ' + this.IconClass,
                border: false
            });
            if (item.children && item.children.length > 0) {
                var panel = ops.$menu.accordion('getPanel', item.text);
                buildTreeMenuCore(panel.find('.navTree'), item.children);
            }
        }
        var panels = ops.$menu.accordion('panels');
        ops.$menu.accordion('select', panels[0].panel('options').title);
    }

    function openTabHandler(hasTab, node) {
        var $tab = ops.$tabs;
        var title = node.text;
        var url = node.url;
        var icon = null;
        if (!hasTab) {
            $tab.tabs('add', {
                id: node.id,
                title: title,
                content: createFrame(url),
                closable: true,
                icon: icon
            });
        } else {
            $tab.tabs('select', subtitle);
        }
    }

    function addTab(node) {
        var title = node.text;
        var url = node.url;
        var icon = null;
        if (!url || url == '#') return false;
        var $tab = ops.$tabs;
        var tabCount = $tab.tabs('tabs').length;
        var hasTab = $tab.tabs('exists', title);
        if ((tabCount <= ops.maxTabCount) || hasTab) {
            openTabHandler(hasTab, node);
        }
        else {
            $.messager.confirm("系统提示", '<b>您当前打开了太多的页面，会造成程序运行缓慢，无法流畅操作！</b>', function (b) {
                if (b) {
                    openTabHandler(hasTab, node);
                }
            });
        }
    }

    function logout() {
        $.messager.confirm('系统提示', '您确定要退出本次登录吗?', function (r) {
            if (r) location.href = 'login.html';
        });
    }

    function getTabTitles() {
        var titles = [];
        var tabs = ops.$tabs.tabs('tabs');
        $.each(tabs, function () {
            titles.push($(this).panel('options').title);
        });
        return titles;
    }

    function onTabsContextMenu(e, title) {
        ops.$tabsmenu.menu('show', {left: e.pageX, top: e.pageY});
        ops.$tabs.tabs('select', title);
        e.preventDefault();
    }

    function tabsMenuClick(item) {
        var $tab = ops.$tabs;
        var currentTab = $tab.tabs('getSelected');
        var titles = getTabTitles($tab);
        var tabIndex = $tab.tabs('getTabIndex', currentTab);
        var currtab_title = currentTab.panel('options').title;
        switch (item.id) {
            case "tabs-menu-refresh":
                var src = $(currentTab.panel('options').content).attr('src');
                if (src) {
                    $tab.tabs('update', {tab: currentTab, options: {content: createFrame(src)}});
                }
                break;
            case "tabs-menu-close":
                if (tabIndex == 0) {
                    return false;
                }
                $tab.tabs('close', currtab_title);
                break;
            case "tabs-menu-closeall":
                $.each(titles, function () {
                    if (this != ops.homeTitle)
                        $tab.tabs('close', this);
                });
                break;
            case "tabs-menu-closeother":
                $.each(titles, function () {
                    if (this != currtab_title && this != ops.homeTitle)
                        $tab.tabs('close', this);
                });
                $tab.tabs('select', 1);
                break;
            case "tabs-menu-closeright":
                if (tabIndex == titles.length - 1) {
                    //alert('亲，后边没有啦 ^@^!!');
                    return false;
                }
                $.each(titles, function (i) {
                    if (i > tabIndex && this != ops.homeTitle)
                        $tab.tabs('close', this);
                });
                break;
            case "tabs-menu-closeleft":
                if (tabIndex == 1) {
                    //alert('亲，前边那个上头有人，咱惹不起哦。 ^@^!!');
                    return false;
                }
                $.each(titles, function (i) {
                    if (i < tabIndex && this != ops.homeTitle)
                        $tab.tabs('close', this);
                });
                break;
            case "tabs-menu-screenfull":
                setFullScreen();
                break;
        }
    }

    function setFullScreen() {
        var self = $('#tabs-menu-screenfull');
        var panels = ops.$body.data().layout.panels;
        if (self.hasClass('full')) {
            self.removeClass('full').addClass('nofull');
            ops.$north.panel('close');
            ops.$west.panel('close');
            ops.$south.panel('close');

            panels.north.length = 0;
            panels.west.length = 0;
            if (panels.expandWest) {
                panels.expandWest.length = 0;
                $(panels.expandWest[0]).panel('close');
            }
            ops.$body.layout('resize');
        } else if (self.hasClass('nofull')) {
            self.removeClass('nofull').addClass('full');
            ops.$north.panel('open');
            ops.$west.panel('open');
            ops.$south.panel('open');

            panels.north.length = 1;
            panels.west.length = 1;
            if ($(panels.west[0]).panel('options').collapsed) {
                panels.expandWest.length = 1;
                $(panels.expandWest[0]).panel('open');
            }
            ops.$body.layout('resize');
        }
    }

    function setHash(title, index) {
        var panel = $(this).tabs('getTab', index);
        var ops = $(panel).panel('options');
        if (ops.id === 'home') {
            return;
        }
        window.location.hash = ops.id;
    }

    return {
        /**
         * 框架初始化
         */
        init: function () {
            $(function () {
                initVar();
                initTime();
                initMenu();
                initLayout();
                initTabs();
            });
        },
        /**
         * 添加标签页
         */
        addTab: function () {

        }
    }
}();

$.admin.init();